package cn.hengzhu.main_manager.web.openAPi.openApiToInner;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.hengzhu.main_manager.repository.dao.ManageApkMapper;
import cn.hengzhu.main_manager.repository.dao.ManageCaseBoxMapper;
import cn.hengzhu.main_manager.repository.dao.ManageCaseUsetimeMapper;
import cn.hengzhu.main_manager.repository.domain.ManageApk;
import cn.hengzhu.main_manager.repository.domain.ManageApkExample;
import cn.hengzhu.main_manager.repository.domain.ManageCaseBox;
import cn.hengzhu.main_manager.repository.domain.ManageCaseBoxExample;
import cn.hengzhu.main_manager.repository.domain.ManageCaseUsetime;
import cn.hengzhu.main_manager.repository.domain.ManageCaseUsetimeExample;
import cn.hengzhu.main_manager.utils.config.AliOssConfig;

/**
 * @author liuCheng
 * @since 2018年11月16日 下午3:17:01 内部的开放接口
 */
@Controller
@RequestMapping("/openApiToInner")
public class OpenController {
	@Autowired
	private ManageCaseUsetimeMapper manageCaseUsetimeMapper;
	@Autowired
	private ManageApkMapper manageApkMapper;
	@Autowired
	private ManageCaseBoxMapper manageCaseBoxMapper;

	/**
	 * @author liuCheng
	 * @since 2018年11月16日 下午3:15:07
	 * @param param
	 * @return 外部请求接口，，，返回使用时间）
	 */
	@ResponseBody
	@RequestMapping("/getUseEndTime")
	public Long getUseEndTime(ManageCaseUsetime param) {
		String caseNo = param.getCaseNo();
		ManageCaseUsetimeExample manageCaseUsetimeExample = new ManageCaseUsetimeExample();
		manageCaseUsetimeExample.createCriteria().andCaseNoEqualTo(caseNo);
		List<ManageCaseUsetime> selectByExample = manageCaseUsetimeMapper.selectByExample(manageCaseUsetimeExample);
		if (selectByExample.isEmpty()) {
			Date date = new Date();
			long time = date.getTime();
			param.setUseEndTime(date);
			param.setCreateTime(date);
			manageCaseUsetimeMapper.insertSelective(param);
			return time;
		} else {
			ManageCaseUsetime manageCaseUsetime = selectByExample.get(0);
			Date useEndTime = manageCaseUsetime.getUseEndTime();
			long time = useEndTime.getTime();
			return time;
		}
	}

	/**
	 * @author liuCheng
	 * @since 2018年11月21日 下午3:07:37
	 * @return app获取APK列表
	 */
	@ResponseBody
	@RequestMapping("/getApkListToInner")
	public List<ManageApk> getApkListToInner() {
		ManageApkExample example = new ManageApkExample();
		example.createCriteria().andIsDelEqualTo((byte) 0);
		List<ManageApk> ManageApkList = manageApkMapper.selectByExample(example);
		ManageApkList.stream().forEach(a -> {
			a.setUrl(AliOssConfig.URL + a.getUrl());
		});
		return ManageApkList;
	}

	/**
	 * @author liuCheng
	 * @since 2018年11月21日 下午3:07:37
	 * @return 验证密码开柜门
	 */
	@ResponseBody
	@RequestMapping("/verifyPwd")
	public Map<String, Object> verifyPassword(String password, String caseNo) {
		Map<String, Object> response = new HashMap<>();
		if (password == null || "".equals(password)) {
			response.put("code", 404);
			response.put("msg", "密码不能为空！");
			return response;
		}
		if ("0-0!!!".equalsIgnoreCase(password)) {
			response.put("msg", "密码不正确！");
			response.put("code", 500);
			return response;
		}
		if (caseNo == null || "".equalsIgnoreCase(caseNo)) {
			response.put("code", 404);
			response.put("msg", "柜子编号不能为空！");
			return response;
		}
		ManageCaseBoxExample example = new ManageCaseBoxExample();
		example.createCriteria().andIsDelEqualTo((byte) 0).andCaseNoEqualTo(caseNo).andPasswordEqualTo(password)
				.andUseStatusEqualTo((byte) 1);
		List<ManageCaseBox> caseBoxs = manageCaseBoxMapper.selectByExample(example);
		if (!caseBoxs.isEmpty()) {
			ManageCaseBox box = caseBoxs.get(0);
			box.setIsHalfwayOpen((byte) 1);
			manageCaseBoxMapper.updateByPrimaryKey(box);
			response.put("code", 200);
			response.put("msg", "密码正确！");
			response.put("doorNum", box.getDoorNum());
			return response;
		}
		response.put("msg", "密码不正确！");
		response.put("code", 500);
		return response;
	}

	/**
	 * @author liuCheng
	 * @since 2018年11月21日 下午3:07:37
	 * @return 获取未使用柜子数量
	 */
	@ResponseBody
	@RequestMapping("/unUseSize")
	public Map<String, Object> unUseSize(String caseNo) {
		Map<String, Object> response = new HashMap<>();
		if (caseNo == null || "".equalsIgnoreCase(caseNo)) {
			response.put("code", 404);
			response.put("msg", "柜子编号不能为空！");
			return response;
		}
		ManageCaseBoxExample example = new ManageCaseBoxExample();
		example.createCriteria().andIsDelEqualTo((byte) 0).andIsConnEqualTo((byte) 1).andUseStatusEqualTo((byte) 0)
				.andCaseNoEqualTo(caseNo);
		int num = 0;
		List<ManageCaseBox> caseBoxs = manageCaseBoxMapper.selectByExample(example);
		if (!caseBoxs.isEmpty()) {
			num = caseBoxs.size();
		}
		response.put("code", 200);
		response.put("msg", "获取成功！");
		response.put("size", num);
		return response;
	}
}
